#ifndef __NONAME_STACK_H__
#define __NONAME_STACK_H__

#define NONAME_LIB_USE

#include <ntifs.h>

#define DEFAULT_STACK_SIZE	64

template <class T>
class kstack {

public:
	kstack();
	kstack(ULONG Size);
	void Release();
	ULONG Push(const T &Val);
	ULONG pop(T &Val);
	ULONG GetStackPointer() {return m_CurrentSP;}
	

private:
	T *m_Stack;
	ULONG m_StackSize;
	ULONG m_CurrentSP;
};


template <class T>
kstack<T>::kstack()
{
	m_StackSize = DEFAULT_STACK_SIZE;
	m_Stack = new T[m_StackSize];

	m_CurrentSP = m_StackSize;
}

template <class T>
kstack<T>::kstack (ULONG Size)
{
	m_StackSize = Size;
	m_Stack = new T[m_StackSize];

	m_CurrentSP = m_StackSize;
}

template <class T>
void kstack<T>::Release ()
{
	if (m_Stack != NULL)
		delete[] m_Stack;
}

template <class T>
ULONG kstack<T>::Push(const T &Val)
{
	T *tmp;
	ULONG i, j;
	if (m_CurrentSP == 0) {

		tmp = new T[m_StackSize * 2];

		for (i = 0, j = m_StackSize; i < m_StackSize; i++, j++) {
			tmp[j] = m_Stack[i];
		}
		delete[] m_Stack;
		m_Stack = tmp;
		m_CurrentSP = m_StackSize;
		m_StackSize *= 2;
	}

	m_Stack[--m_CurrentSP] = Val;
	return m_CurrentSP;
}

template <class T>
ULONG kstack<T>::pop (T &val)
{
	if (m_CurrentSP == m_StackSize) {
		return 0;
	}
	val = m_Stack[cur_sp++];

	return m_CurrentSP;
}

#endif